package agent.client.android;

import jade.core.MicroRuntime;
import jade.wrapper.ControllerException;
import jade.wrapper.StaleProxyException;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import agent.client.android.agent.AgentInterface;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class SettingsActivity extends Activity
{
	
	private Logger logger = Logger.getLogger(this.getClass().getName());
	String dataTransferObject;
	EditText titolo;
	EditText descrizione;
	EditText luogo;
	EditText invitato;
	Spinner spinner;
	
	private String username;
	private AgentInterface agentInterface;
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		logger.info("onCreate SettingsActivity");
		dataTransferObject = (String) getIntent().getExtras().get("dto");
		username = getIntent().getExtras().getString("username");
		logger.info("onCreate "+username+" "+dataTransferObject);
		try {
			agentInterface = MicroRuntime.getAgent(username).getO2AInterface(AgentInterface.class);
			
			logger.info("agentInterface ");
			
			if(agentInterface==null)
				logger.info("NULLO");
			
			setContentView(R.layout.settings);

			titolo = ( EditText ) findViewById(R.id.titolo);
			descrizione = ( EditText ) findViewById(R.id.descrizione);
			luogo = ( EditText ) findViewById(R.id.luogo);
			invitato = ( EditText ) findViewById(R.id.invitato);
			
		    spinner = (Spinner) findViewById(R.id.spinner);
		    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
		            this, R.array.hour_array, android.R.layout.simple_spinner_item);
		    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		    spinner.setAdapter(adapter);

		    
		   
		    Button button = (Button)findViewById(R.id.continua);
		    button.setOnClickListener(buttonListener);
		} catch (StaleProxyException e) {
			logger.info("onCreate Error Arent not Found1");
			e.printStackTrace();
		} catch (ControllerException e) {
			logger.info("onCreate Error Arent not Found2");
			e.printStackTrace();
		}
		
		
	}
	

	private OnClickListener buttonListener=new OnClickListener() {
		
		public void onClick(View v) {
			
			
			
			dataTransferObject+= descrizione.getText().toString()+",";
			dataTransferObject+= titolo.getText().toString()+",";
			dataTransferObject+= luogo.getText().toString()+",";
					
			List<String> receivers=new ArrayList<String>();
			receivers.add(invitato.getText().toString());
			
			dataTransferObject+= receivers.get(0)+",";
			
			int pos = spinner.getSelectedItemPosition();
			
			long dur = (1800000 + pos*1800000);
			
			dataTransferObject += dur;
			
			logger.info(Long.toString(pos));
			System.out.println(pos);
			
			agentInterface.requestAppointment(dataTransferObject);
			
			Toast.makeText(getApplicationContext(), "STIAMO LAVORANDO PER TE.. ", Toast.LENGTH_SHORT).show();
			
			Intent intent = new Intent(SettingsActivity.this, HomeActivity.class);
			
			intent.putExtra("username", (String)getIntent().getExtras().get("username"));
			
			startActivity(intent);
			
			}
		
	};

//	@Override
//	protected void onStop() {
//		// TODO Auto-generated method stub
//		super.onStop();
//		
//		try {
//			MicroRuntime.getAgent(username).kill();
//		} catch (StaleProxyException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (ControllerException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	
	

}
